package com.zhuiyun.project.api.lottery.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.zhuiyun.project.api.lottery.entity.Args;
import com.zhuiyun.project.api.lottery.entity.Lottery;
import com.zhuiyun.project.api.lottery.entity.PLSResult;
import com.zhuiyun.project.api.lottery.service.LotteryService;
import com.zhuiyun.project.api.lottery.task.AsyncLotteryData;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.RedisUtil;
import com.zhuiyun.project.util.UserUtils;
import com.zhuiyun.project.util.constants.CommonConstants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@RestController
public class LotteryController extends BaseController {

    @Autowired
    LotteryService lotteryService;
    private final ReentrantLock lock = new ReentrantLock();


    @Autowired
    AsyncLotteryData asyncLotteryData;
    /**
     * @param args  彩票得类型  百位 / 12345
     * @return
     */
    @GetMapping("/hundredOne")
    public CommonResult hundredOne(Args args){
        PLSResult hundredOneData = null;
                switch (args.getOption()){
                    //百十个位
                    case 0:
                        hundredOneData=lotteryService.getLotteryHTP(args);
                        break;
                    // 百位 /12345 这里
                    case 1:
                    case 2:
                    case 3:
                        hundredOneData = lotteryService.getHundredOne(args);
                        break;
                    case 4:
                    case 5:
                    case 6:
                        // 百位 \12345 这里
                        hundredOneData = lotteryService.getHundredOneReverse(args);
                        break;
                    case 7:
                        // 百十个位差
                        hundredOneData = lotteryService.getLotteryHTPDifference(args);
                        break;
                    // 百十差 十个差 百个差 这里 /12345
                    case 8:
                    case 9:
                    case 10:
                        hundredOneData=lotteryService.getHundredTenDifference(args);
                        break;
                    // 百十个位和
                    case 11:
                        hundredOneData=lotteryService.getLotteryHTPSum(args);
                        break;
                    // 百十差 十个差 百个差 这里 \12345
                    case 12:
                    case 13:
                    case 14:
                        hundredOneData=lotteryService.getHundredTenDifferenceRollBack(args);
                        break;
                    // 小中大
                    case 15:
                        hundredOneData=lotteryService.getLotteryHTPOrderBy(args);
                        break;
                    // 百十和 十个和 百个和 这里 /12345
                    case 16:
                    case 17:
                    case 18:
                        hundredOneData=lotteryService.getHundredTenSum(args);
                        break;
                    // 小中大差
                    case 19:
                        hundredOneData=lotteryService.getLotteryHTPOrderByDifference(args);
                        break;
                    // 百十和 十个和 百个和 这里 \12345
                    case 20:
                    case 21:
                    case 22:
                        hundredOneData=lotteryService.getHundredTenSumRollBack(args);
                        break;
                    // 小中大和
                    case 23:
                        hundredOneData=lotteryService.getLotteryHTPOrderBySum(args);
                        break;
                    // 小中大 /12345
                    case 24:
                    case 25:
                    case 26:
                        hundredOneData=lotteryService.getSmallMiddleBig(args);
                        break;
                    //组选通杀
                    case 27:
                        hundredOneData=lotteryService.getLotteryHTP(args);
                        break;
                    // 小中大 \12345
                    case 28:
                    case 29:
                    case 30:
                        hundredOneData=lotteryService.getSmallMiddleBigRollBack(args);
                        break;
                    //振幅差
                    case 31:
                        hundredOneData=lotteryService.getLotteryHTPOrderByAmplitudeDifference(args);
                        break;
                    // 小中差、中大差、小大差 /12345
                    case 32:
                    case 33:
                    case 34:
                        hundredOneData=lotteryService.getSmallMiddleBigDifference(args);
                        break;
                    //振幅和
                    case 35:
                        hundredOneData=lotteryService.getLotteryHTPOrderByAmplitudeSum(args);
                        break;
                    // 小中差、中大差、小大差 \12345
                    case 36:
                    case 37:
                    case 38:
                        hundredOneData=lotteryService.getSmallMiddleBigDifferenceRollBack(args);
                        break;
                        // 小中和、中大和、小大和 /12345
                    case 40:
                    case 41:
                    case 42:
                        hundredOneData=lotteryService.getSmallMiddleBigSum(args);
                        break;
                        //和尾
                    case 43:
                        hundredOneData=lotteryService.getLotteryHTPAndTails(args);
                        break;
                    // 小中和、中大和、小大和 \12345
                    case 44:
                    case 45:
                    case 46:
                        hundredOneData=lotteryService.getSmallMiddleBigSumRollBack(args);
                        break;
                    // 1-8胆 /12345
                    case 49:
                    case 64:
                        hundredOneData=lotteryService.getOneAndEight(args);
                        break;
                    // 1-8胆 \12345
                    case 53:
                    case 68:
                        hundredOneData=lotteryService.getOneAndEightRollBack(args);
                        break;
                    // 3-7胆 /12345
                    case 51:
                    case 59:
                        hundredOneData=lotteryService.getThreeAndSeven(args);
                        break;
                    // 3-7胆 \12345
                    case 55:
                    case 63:
                        hundredOneData=lotteryService.getThreeAndSevenRollBack(args);
                        break;
                    // 5-6胆 /12345
                    case 57:
                    case 58:
                        hundredOneData=lotteryService.getFiveAndSix(args);
                        break;
                    // 5-6胆 \12345
                    case 61:
                    case 62:
                        hundredOneData=lotteryService.getFiveAndSixRollBack(args);
                        break;
                    //单选间隔
                    case 66:
                        hundredOneData=lotteryService.getSingleChoiceInterval(args);
                        break;
                    // 4-2胆 /12345
                    case 56:
                    case 50:
                        hundredOneData=lotteryService.getFourAndTwo(args);
                        break;
                    // 4-2胆 \12345
                    case 60:
                    case 54:
                        hundredOneData=lotteryService.getFourAndTwoRollBack(args);
                        break;
                    // 0-9胆 /12345
                    case 48:
                    case 65:
                        hundredOneData=lotteryService.getZeroAndNine(args);
                        break;
                    // 0-9胆 \12345
                    case 52:
                    case 69:
                        hundredOneData=lotteryService.getZeroAndNineRollBack(args);
                        break;
                    //号码组合形态
                    case 70:
                        hundredOneData=lotteryService.getNumberCombination(args);
                        break;
                    // 1-8差胆 /12345
                    case 73:
                    case 88:
                        hundredOneData=lotteryService.getOneAndEightDifference(args);
                        break;
                    // 1-8差胆 \12345
                    case 77:
                    case 92:
                        hundredOneData=lotteryService.getOneAndEightDifferenceRollBack(args);
                        break;
                    // 3-7差胆 /12345
                    case 75:
                    case 83:
                        hundredOneData=lotteryService.getThreeAndSevenDifference(args);
                        break;
                    // 3-7差胆 \12345
                    case 79:
                    case 87:
                        hundredOneData=lotteryService.getThreeAndSevenDifferenceRollBack(args);
                        break;
                    // 5-6差胆 /12345
                    case 81:
                    case 82:
                        hundredOneData=lotteryService.getFiveAndSixDifference(args);
                        break;
                    // 5-6差胆 \12345
                    case 85:
                    case 86:
                        hundredOneData=lotteryService.getFiveAndSixDifferenceRollBack(args);
                        break;
                    // 4-2差胆 /12345
                    case 80:
                    case 74:
                        hundredOneData=lotteryService.getFourAndTwoDifference(args);
                        break;
                    // 4-2差胆 \12345
                    case 84:
                    case 78:
                        hundredOneData=lotteryService.getFourAndTwoDifferenceRollBack(args);
                        break;
                    // 0-9差胆 /12345
                    case 72:
                    case 89:
                        hundredOneData=lotteryService.getZeroAndNineDifference(args);
                        break;
                    // 0-9差胆 \12345
                    case 76:
                    case 93:
                        hundredOneData=lotteryService.getZeroAndNineDifferenceRollBack(args);
                        break;
            }

        return CommonResult.ok(hundredOneData);
    }


    /**
     * @param args  百位 / 12345 按钮 数列数据组合
     * @return
     */
    @GetMapping("/hundredOTSTF")
    public CommonResult hundredOTSTF(Args args){
        PLSResult  hundredOneData = lotteryService.hundredOTSTF(args);
        return CommonResult.ok(hundredOneData);
    }

    /**
     * 获取所有彩票的期号
     * @param type
     * @return
     */
    @GetMapping("/getLotteryNo")
    public CommonResult getLotteryNo(String type){
        List<Map> list = lotteryService.getLotteryNo(type);
        return CommonResult.ok(list);
    }

    /**
     * 获取彩票的最新期数
     * @param type
     * @return
     */
    @GetMapping("/getLastLotteryNo")
    public CommonResult getLastLotteryNo(String type){
       String  lastLotteryNo = lotteryService.getLastLotteryNo(type);
        return CommonResult.ok(lastLotteryNo);
    }
    /**
     * 获取彩票的最新奖号
     * @param type
     * @return
     */
    @GetMapping("/getLastLotteryRes")
    public CommonResult getLastLotteryRes(String type){
        String  lastLotteryRes = lotteryService.getLastLotteryRes(type);
        return CommonResult.ok(lastLotteryRes);
    }

    /**
     * 接龙数据记录
     * @param type
     * @return
     */
    @PostMapping("/setJlByTypeAndPhone")
    public CommonResult setJlByTypeAndPhone(@RequestBody Map<String,Object> map){
        String phone = UserUtils.getUser().getPhone();
        String type = (String)map.get("type");
        ArrayList selectedCellsList = (ArrayList) map.get("selectedCells");
        ArrayList mySelectedCells = (ArrayList) map.get("mySelectedCells");
        lock.lock();
        try {
            ArrayList  selectedCells =  (ArrayList)RedisUtil.get(type);
            //如果有值判断再并发情况已经有人改动 如果有人改动则修改失败
            if(ObjectUtils.isNotEmpty(selectedCells)){
               if((selectedCells.size()-selectedCellsList.size())==3){
                   RedisUtil.set(type,selectedCellsList);
                   RedisUtil.hset(CommonConstants.JL+type,phone,mySelectedCells);
               }else{
                   return CommonResult.error(EmErrorCode.READ_CHANGE);
               }
            }else{ // 如果redis中没有值说明首次提交 直接通过
                RedisUtil.hset(CommonConstants.JL+type,phone,mySelectedCells);
                RedisUtil.set(type,selectedCellsList);
            }
        } finally {
            lock.unlock(); // 确保释放锁
        }
        return CommonResult.ok();
    }

    /**
     * 获取接龙数据
     * @param type
     * @return
     */
    @GetMapping("/getJlByTypeAndPhone")
    public CommonResult getJlByTypeAndPhone(String type){
        String phone = UserUtils.getUser().getPhone();
        ArrayList mySelectedCells = (ArrayList) RedisUtil.hget(CommonConstants.JL+type,phone);
        ArrayList  selectedCells =  (ArrayList)RedisUtil.get(type);
        Map h = new HashMap();
        if(ObjectUtils.isNotEmpty(mySelectedCells)){
            h.put("mySelectedCells",mySelectedCells);
        }else{
            h.put("mySelectedCells",new ArrayList<>());
        }
        if(ObjectUtils.isNotEmpty(selectedCells)){
            h.put("selectedCells",selectedCells);
        }else{
            h.put("selectedCells",new ArrayList<>());
        }
        return CommonResult.ok(h);
    }

    /**
     * 获取所有类型彩票的开奖结果
     * @param
     * @return
     */
    @GetMapping("/getAllTypeLottery")
    public CommonResult getAllTypeLottery(){
       Map<Object, Object> allTypeLottery =RedisUtil.hmget(CommonConstants.CPTYPES);
       if(!ObjectUtils.isNotEmpty(allTypeLottery)){
           asyncLotteryData.asyncAllType();
           allTypeLottery = RedisUtil.hmget(CommonConstants.CPTYPES);
       }
        return CommonResult.ok(allTypeLottery);
    }
}
